Logistic Regression using Scikit-Learn

python
deep learning.ai
machine learning
supervised learning
logistic regression
Author

kakamana

Published

April 25, 2023

Logistic Regression using Scikit-Learn

will show you how to use the popular sci-kit learn library to train a logistic regression model for classification.

After you run gradient descent in the lab, there will be a nice set of animated plots that show gradient descent in action. You’ll see the sigmoid function, the contour plot of the cost, the 3D surface plot of the cost, and the learning curve all evolve as gradient descent runs

This Logistic Regression using Scikit-Learn is part of DeepLearning.AI course: Machine Learning Specialization / Course 1: Supervised Machine Learning: Regression and Classification In this course we will learn the difference between supervised and unsupervised learning and regression and classification tasks. Develop a linear regression model. Understand and implement the purpose of a cost function. Understand and implement gradient descent as a machine learning training method.

This is my learning experience of data science through DeepLearning.AI. These repository contributions are part of my learning journey through my graduate program masters of applied data sciences (MADS) at University Of Michigan, DeepLearning.AI, Coursera & DataCamp. You can find my similar articles & more stories at my medium & LinkedIn profile. I am available at kaggle & github blogs & github repos. Thank you for your motivation, support & valuable feedback.

These include projects, coursework & notebook which I learned through my data science journey. They are created for reproducible & future reference purpose only. All source code, slides or screenshot are intellectual property of respective content authors. If you find these contents beneficial, kindly consider learning subscription from DeepLearning.AI Subscription, Coursera, DataCamp

Lab: Logistic Regression using Scikit-Learn

Goals

In this lab you will: - Train a logistic regression model using scikit-learn.

Dataset

Let’s start with the same dataset as before.

Code
import numpy as np

X = np.array([[0.5, 1.5], [1,1], [1.5, 0.5], [3, 0.5], [2, 2], [1, 2.5]])
y = np.array([0, 0, 0, 1, 1, 1])

Fit the model

The code below imports the logistic regression model from scikit-learn. You can fit this model on the training data by calling fit function.

Code
from sklearn.linear_model import LogisticRegression

lr_model = LogisticRegression()
lr_model.fit(X, y)
LogisticRegression()
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.

Make Predictions

You can see the predictions made by this model by calling the predict function.

Code
y_pred = lr_model.predict(X)

print("Prediction on training set:", y_pred)
Prediction on training set: [0 0 0 1 1 1]

Calculate accuracy

You can calculate this accuracy of this model by calling the score function.

Code
print("Accuracy on training set:", lr_model.score(X, y))
Accuracy on training set: 1.0